﻿
$(document).ready(function () {
    $('.easyui-tabs').tabs({
        onSelect: function (title) {
            if (title === "图集") {
                $('#thumbnails').sortable({}).bind('sortupdate', function () {
                    //Message("排序已经更新。");
                    moveOrderIdPhotoList();
                });
            }
        }
    });
    $("#Form1").validate({
        rules: { ddlCategoryId: "required", txtTitle: "required" },
        messages: { ddlCategoryId: " 必填项", txtTitle: " 必填项" }
    });

    $("#UP_PicUrl").uploadify({
        height: 30,
        'multi': false,
        swf: '/Scripts/Uploadify/uploadify.swf',
        uploader: '/Scripts/Uploadify/UploadHandler.ashx',
        width: 120,
        cancelImg: '/Scripts/Uploadify/uploadify-cancel.png',
        buttonText: '上传图片',
        fileTypeExts: '*.gif;*.jpg;*.jpeg;*.png',
        fileSizeLimit: '6000KB',
        removeCompleted: true,
        formData: {
            PicW: $("#txtPicW").val(),
            PicH: $("#txtPicH").val(),
            BiLi: $("#cbkBiLi").prop("checked")
        },
        onUploadSuccess: function (file, data, response) {
            //上传完成时触发（每个文件触发一次）
            if (data.indexOf('错误提示') > -1) {
                alert(data);
            } else {
                $("#txtPicUrl").val(data);
            }
        },
        onUploadError: function (file, errorCode, errorMsg, errorString) {
            //当单个文件上传出错时触发
            alert('文件：' + file.name + ' 上传失败: ' + errorString);
        }
    });

    $("#UP_PhotoList").uploadify({
        height: 30,
        'multi': true,
        swf: '/Scripts/Uploadify/uploadify.swf',
        uploader: '/api/Photo/Upload',
        width: 120,
        cancelImg: '/Scripts/Uploadify/uploadify-cancel.png',
        buttonText: '上传图片',
        fileTypeExts: '*.gif;*.jpg;*.png;*.swf',
        fileSizeLimit: '6000KB',
        removeCompleted: true,
        'debug': false,
        formData: {
            ContentId: Request.QueryString("ID") == null ? -1 : Request.QueryString("ID"),
            ModelId: Request.QueryString("ModelId"),
            TempFlag: Request.QueryString("TempFlag") == null ? "" : Request.QueryString("TempFlag")
        },
        onUploadSuccess: function (file, data, response) {
            if (data.indexOf('错误提示') > -1) {
                alert(data);
            } else {
                var info = $.parseJSON(data);
                PhotoListItem(info);
            }
        },
        onUploadError: function (file, errorCode, errorMsg, errorString) {
            alert('文件：' + file.name + ' 上传失败: ' + errorString);
        }
    });

    loadPhotoList();


  

    //$('#thumbnails').sortable({
    //    start: function (event, ui) {
    //        ui.item.addClass('active');
    //    },
    //    stop: function (event, ui) {
    //        ui.item.removeClass('active').effect(
    //            'highlight',
    //            { color: '#000' }, 1000, function () {
    //                $.each($('.thumbshow'), function (index, e) {
    //                    $(this).children('span').html(index + 1);
    //                });
    //            });
    //        moveOrderIdPhotoList();
    //    }

    //});
    //$('#thumbnails').disableSelection();
});

function ColorPalette() {
    var str = window.showModalDialog("../Images/JS拾色器/JS拾色器.htm", "dialogWidth=190px;dialogHeight=80px");
    if (typeof (str) != "undefined") {
        $get('txtColorStyle').value = str;
        LoadStyle();
    }
}
function LoadStyle() {
    if ($get('txtColorStyle') == null) {
        return;
    }
    if ($get('imgColorStyle') == null) {
        return;
    }
    var str = $get('txtColorStyle').value;
    if (str == "") {
        $get('imgColorStyle').style.backgroundColor = "#000000";
    }
    else {
        $get('imgColorStyle').style.backgroundColor = str;
    }
}

function GetImage() {
    $("#dialog-modal").dialog({
        height: 540,
        width: 540,
        title: "图片浏览",
        modal: true
    });
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetImage.aspx");
    urlC.width(500);
    urlC.height(500);
}
function GetTemplate() {
    $("#dialog-modal").dialog({
        height: 450,
        width: 500,
        title: "网页模板",
        modal: true
    });
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetTemplate.aspx?Control=txtTemplateFileName");
    urlC.width(460);
    urlC.height(410);
}
function GetTag(modelId) {
    $("#dialog-modal").dialog({
        height: 450,
        width: 500,
        title: "Tags选用",
        modal: true
    });
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetTag.aspx?ModelId=" + modelId + "&Control=txtKey");
    urlC.width(460);
    urlC.height(410);
}

function AuthorMore() {
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetContentProperty.aspx?ModelId=" + Request.QueryString("ModelId") + "&Type=1&Control=txtAuthor");
    urlC.width(460);
    urlC.height(410);

    $("#dialog-modal").dialog({
        height: 450,
        width: 500,
        title: "作者选用",
        modal: true
    });
}

function FromMore() {
    var urlC = $("#dialog-modal-url");
    urlC.attr("src", "../window/GetContentProperty.aspx?ModelId=" + Request.QueryString("ModelId") + "&Type=2&Control=txtFrom");
    urlC.width(460);
    urlC.height(410);

    $("#dialog-modal").dialog({
        height: 450,
        width: 500,
        title: "文章来源",
        modal: true
    });
}

function CheckTitle() {
    $.ajax({
        type: "post",
        dataType: "JSON",
        url: "../DataHandler.ashx?Action=CheckTitle&Title=" + escape($("#txtTitle").val()) + "&ModelId=" + Request.QueryString("ModelId"),
        success: function (data) {
            alert(data.Msg);
        }
    });
}


function loadPhotoList() {
    var id = Request.QueryString("ID") == null ? 0 : Request.QueryString("ID");
    var tempFlag = Request.QueryString("TempFlag") == null ? "" : Request.QueryString("TempFlag");
    $.ajax({
        type: "get",
        dataType: "JSON",
        url: "/api/Photo/Load?contentId=" + id + "&tempFlag=" + tempFlag,
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                PhotoListItem(data[i]);
            }
        }
    });
}

function PhotoListItem(info) {
    $("#thumbnails").append("<div class=\"thumbshow\" id=\"thumbshow" + info.Id + "\" datafld=\"" + info.Id + "\"><span>" + info.OrderId + "</span><a target=\"_blank\" href=\"" + info.PicUrl + "\"><img src=\"" + info.ThumbUrl + "\" alt=\"\" /></a><div style=\"margin-top: 1px; text-align: left\"><a style=\"color: #ff6600\" href=\"javascript:deletePhotoList(" + info.Id + ")\">[删除]</a><input type=\"text\" style=\"width: 150px;\" value=\"" + info.Title + "\" datafld=\"" + info.Id + "\" id=\"inpTitle" + info.Id + "\" placeholder=\"注释\" /></div></div>");


    $("#inpTitle" + info.Id).change(updatePhotoListTitle);
}

function deletePhotoList(id) {
    $.ajax({
        type: "Delete",
        dataType: "JSON",
        url: '/api/Photo/Delete/' + id,
 
        success: function (data) {
            $("#thumbshow" + id).remove();
        }
    });
}

function updatePhotoListTitle(event) {
    var id = $(event.target).attr("datafld");
    var title = $(event.target).val();

    $.ajax({
        type: "post",
        url: "/api/Photo/UpdatetTitle?id=" + id + "&title=" + title,
        success: function (data) {
        }
    });
}
function moveOrderIdPhotoList() {
    var info = {
        "Ids": [],
        "ContentId": 0,
        "TempFlag": ""
    };

    $(".thumbshow").each(function () {
        info.Ids.push($(this).attr("datafld"));
    });
    info.ContentId = (Request.QueryString("ID") == null ? -1 : Request.QueryString("ID"));
    info.TempFlag = (Request.QueryString("TempFlag") == null ? "" : Request.QueryString("TempFlag"));

    $.ajax({
        type: "post",
        dataType: "JSON",
        url: "/api/Photo/MoveOrderId",
        data: info,
        success: function (result) {
            if (result.errors != "") {
                alert(result.errors);
            }
        }
    });
}